/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-02-15     tinnu       the first version
 */

#include "my_sys.h"
#include "at32f4xx.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void delay_ms(uint32_t n)
{
        uint32_t temp,i=0;

#ifndef _SYSTICK_8DIV_
    SysTick->LOAD=(uint32_t)SystemCoreClock/1000;               //Ê±¼ä¼ÓÔØ(SysTick->LOADÎª24bit)
    SysTick->CTRL|=SysTick_CTRL_CLKSOURCE_Msk;
#else
    SysTick->LOAD=(uint32_t)SystemCoreClock/8000;
    SysTick->CTRL&=(~SysTick_CTRL_CLKSOURCE_Msk);
#endif

    for(i=0;i<n;i++)
    {
        SysTick->VAL =0x00;                         //Çå¿Õ¼ÆÊýÆ÷
        SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;    //¿ªÊ¼µ¹Êý
        do
        {
            temp=SysTick->CTRL;
        }while((temp&0x01)&&!(temp&(1<<16)));       //µÈ´ýÊ±¼äµ½´ï
        SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;    //¹Ø±Õ¼ÆÊýÆ÷
        SysTick->VAL =0X00;                         //Çå¿Õ¼ÆÊýÆ÷
    }
}

void delay_us(uint32_t n)
{
    for(int i=0; i<=n; i++)
        for(int j=0; j<=100; j++)
            __NOP();
}

void PRINT_UART1(char *t_char)
{
    int len = strlen(t_char);
    for(int i=0; i<len; i++)
    {
        USART_SendData(USART1, t_char[i]);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TDE) == RESET);
    }
}


/**
  * @brief  Configure LED GPIO
  * @param  led: Specifies the LED to be configured.
  * @retval None
  */
void AT32_LEDn_Init()
{
  GPIO_InitType GPIO_InitStructure;
  /*Enable the LED Clock*/
  RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOD, ENABLE);

  /*Configure the LED pin as ouput push-pull*/
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pins = GPIO_Pins_13|GPIO_Pins_14|GPIO_Pins_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
  GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}

/**
  * @brief  Turns selected LED On.
  * @param  Led: Specifies the Led to be set on.
  *   This parameter can be one of following parameters:
  *     @arg LED1
  *     @arg LED2
  *     @arg LED3
  *     @arg LED4
  * @retval None
  */
void AT32_LEDn_ON(LED_Type led)
{
  if ( led > (LED_NUM - 1))
    return;
  switch(led){
//  case LED1:
//      LED1_GPIO->BRE = LED1_PIN;
//      break;
  case LED2:
      LED2_GPIO->BRE = LED2_PIN;
      break;
  case LED3:
      LED3_GPIO->BRE = LED3_PIN;
      break;
  case LED4:
      LED4_GPIO->BRE = LED4_PIN;
      break;
  default:
      break;
  }
}

/**
  * @brief  Turns selected LED Off.
  * @param  Led: Specifies the Led to be set off.
  *   This parameter can be one of following parameters:
  *     @arg LED1
  *     @arg LED2
  *     @arg LED3
  *     @arg LED4
  * @retval None
  */
void AT32_LEDn_OFF(LED_Type led)
{
  if ( led > (LED_NUM - 1))
    return;
  switch(led){
//  case LED1:
//      LED1_GPIO->BSRE = LED1_PIN;
//      break;
  case LED2:
      LED2_GPIO->BSRE = LED2_PIN;
      break;
  case LED3:
      LED3_GPIO->BSRE = LED3_PIN;
      break;
  case LED4:
      LED4_GPIO->BSRE = LED4_PIN;
      break;
  default:
      break;
  }
}

/**
  * @brief  Turns selected LED Tooggle.
  * @param  Led: Specifies the Led to be set off.
  *   This parameter can be one of following parameters:
  *     @arg LED1
  *     @arg LED2
  *     @arg LED3
  *     @arg LED4
  * @retval None
  */
void AT32_LEDn_Toggle(LED_Type led)
{
  if ( led > (LED_NUM - 1))
    return;
  switch(led){
//  case LED1:
//      LED1_GPIO->OPTDT ^= LED1_PIN;
//      break;
  case LED2:
      LED2_GPIO->OPTDT ^= LED2_PIN;
      break;
  case LED3:
      LED3_GPIO->OPTDT ^= LED3_PIN;
      break;
  case LED4:
      LED4_GPIO->OPTDT ^= LED4_PIN;
      break;
  default:
      break;
  }
}


int ftoa(char *str, double num)
{
    int     sumI;
    float   sumF;
    int     sign = 0;
    int     temp;
    int     count = 0;
    int     n=2;    //保留多少位

    char *p;
    char *pp;

    if(str == NULL) return -1;
    p = str;

    /*Is less than 0*/
    if(num < 0)
    {
        sign = 1;
        num = 0 - num;
    }

    sumI = (int)num;    //sumI is the part of int
    sumF = num - sumI;  //sumF is the part of float

    /*Int ===> String*/
    do
    {
        temp = sumI % 10;
        *(str++) = temp + '0';
    }while((sumI = sumI /10) != 0);



    /*******End*******/




    if(sign == 1)
    {
        *(str++) = '-';
    }

    pp = str;

    pp--;
    while(p < pp)
    {
        *p = *p + *pp;
        *pp = *p - *pp;
        *p = *p -*pp;
        p++;
        pp--;
    }

    *(str++) = '.';     //point

    /*Float ===> String*/
    do
    {
        temp = (int)(sumF*10);
        *(str++) = temp + '0';

        if((++count) == n)
            break;

        sumF = sumF*10 - temp;

    }while(!(sumF > -0.000001 && sumF < 0.000001));

    *str = '/0';

    return 0;
}

